$(document).ready(()=>{
    let clearform = ()=>{
        $("#goodName").val("");
        $("#goodPrice").val("");
        $("#goodType").val("");
        $("#goodState").val("");
    }
    //表单的验证及提交
    $("#btn").click(()=>{
        let goodName = $("#goodName").val();
        let goodPrice = $("#goodPrice").val();
        let goodSaler = JSON.parse(localStorage.getItem("admin")).userName;
        let goodType = $("#goodType").val();
        let goodState = $("#goodState").val();
        let goodimgPath = localStorage.getItem("imgPath");
        if(goodName === ""){
            alert("商品名称是必填项，岂能为空？");
        }else if(goodPrice === ""){
            alert("商品价格是必填项，岂能为空？");
        }else if(goodSaler === ""){
            alert("商家是必填项，岂能为空？");
        }else if(goodType === ""){
            alert("商品分类是必填项，岂能为空？");
        }else if(goodState === ""){
            alert("商品描述是必填项，岂能为空？");
        }else if(goodimgPath === "" || goodimgPath === null){
            alert("请上传商品图片");
        }else{
            let data = $("#form").serialize() + "&goodImgPath="+ goodimgPath+"&goodSaler="+goodSaler;
            data = decodeURIComponent(data,true)
            $.ajax({
                url:'http://127.0.0.1:2021/shopstore/good/saveGood',
                data:data,
                type:'post',
                headers: {
                    'token': JSON.parse(localStorage.getItem('token')),
                    'userId': JSON.parse(localStorage.getItem('admin')).userId
                },
                async: true,
                datatype: 'json',
                success: (resp) =>{
                    if(resp.status){
                        alert(resp.message);
                        clearform();
                    }else{
                        alert(resp.message);
                    }
                },
                error: (error) =>{
                    console.log(error)
                }
            })
        }
    })
    //导入按钮链接
    $("#goodinto").click(()=>{
        window.location.href="../iframe/jd_search.html";
    })
})